package com.example.edu_service.controller;


import com.example.commonutils.R;
import com.example.edu_service.client.VodClient;
import com.example.edu_service.entity.EduChapter;
import com.example.edu_service.entity.EduVideo;
import com.example.edu_service.service.EduVideoService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/edu_service/edu_video")

public class EduVideoController {

    @Autowired
    private EduVideoService videoService;


    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    @GetMapping("getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable String videoId){
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("video",video);
    }



    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
         videoService.updateById(eduVideo);

         return R.ok();
    }

    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id){
        //通过id得到video
        EduVideo eduVideo = videoService.getById(id);
        //得到视频id
        String videoSourceId = eduVideo.getVideoSourceId();
        //视频id没有不删除，有再删除
        if (StringUtils.hasLength(videoSourceId)){
            vodClient.deleteVideo(videoSourceId);
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

}

